package com.example.service;

import com.example.mapper.PostMapper;
import com.example.mapper.PostsMapper;
import com.example.mapper.RecruiterMapper;
import com.example.pojo.dto.BossDTO;
import com.example.pojo.dto.CollectDTO;
import com.example.pojo.dto.PostDTO;
import com.example.pojo.entity.JobRecruiter;
import com.example.pojo.entity.Post;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 本文来演示:
 *
 * @author 赵建云
 * @create 2023/9/30
 */
@Service
@Transactional
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostsMapper postsMapper;

    @Autowired
    private RecruiterMapper recruiterMapper;

    @Override
    public List<Post> findAllPost(Integer jrId) {
        List<Post> postList = postMapper.findAllPost(jrId);
        return postList;
    }

    @Override
    public PageInfo<Post> findAllByPage(Integer jrId, Integer pageNum, Integer pageSize) {
        PageInfo<Post> pageInfo = null;
        Page<Post> page = PageHelper.startPage(pageNum, pageSize);
        List<Post> postList = postMapper.findAllPost(jrId);
        pageInfo = new PageInfo<>(postList);
        return pageInfo;
    }

    @Override
    public int save(Post post) {

        PostDTO postDTO = new PostDTO();
        postDTO.setJrId(post.getJrId());
        postDTO.setAgeDemand(post.getAgeDemand());
        postDTO.setJobBenefits(post.getJobBenefits());
        postDTO.setPayFloor(post.getPayFloor());
        postDTO.setExpDemand(post.getExpDemand());
        postDTO.setPosEmail(post.getPosEmail());
        postDTO.setPosName(post.getPosName());
        postDTO.setPosTelephone(post.getPosTelephone());
        postDTO.setPayUpper(post.getPayUpper());
        postDTO.setPosDescription(post.getPosDescription());
        postDTO.setPosType(post.getPosType());
        postDTO.setSpecialty(post.getSpecialty());

        postDTO.setPublishCycle(post.getPublishCycle());

        postDTO.setRecruitsNum(post.getRecruitsNum());
        postDTO.setPosKeyword(post.getPosKeyword());
        if (post.getBackground().contains("本科")){
            postDTO.setBackground(1);
        } else if (post.getBackground().equals("不限")) {
            postDTO.setBackground(0);
        }else if (post.getBackground().contains("硕士")){
            postDTO.setBackground(2);
        }else {
            postDTO.setBackground(3);
        }
        if (post.getSexDemand().equals("男")){
            postDTO.setSexDemand("1");
        }else if (post.getSexDemand().equals("女")){
            postDTO.setSexDemand("2");
        }else {
            postDTO.setSexDemand("0");
        }
        postDTO.setJobBenefits(post.getJobBenefits());
        postDTO.setWorkPlace(post.getWorkPlace());
        int count = postMapper.save(postDTO);
        return count;
    }

    @Override
    public int update(Post post) {
        PostDTO postDTO = new PostDTO();
        postDTO.setJrId(postDTO.getJrId());
        postDTO.setAgeDemand(post.getAgeDemand());
        postDTO.setJobBenefits(post.getJobBenefits());
        postDTO.setPayFloor(post.getPayFloor());
        postDTO.setExpDemand(post.getExpDemand());
        postDTO.setPosEmail(post.getPosEmail());
        postDTO.setPosName(post.getPosName());
        postDTO.setPosTelephone(post.getPosTelephone());
        postDTO.setPayUpper(post.getPayUpper());
        postDTO.setPosDescription(post.getPosDescription());
        postDTO.setPosType(post.getPosType());
        postDTO.setSpecialty(post.getSpecialty());
        postDTO.setPublishCycle(post.getPublishCycle());
        postDTO.setRecruitsNum(post.getRecruitsNum());
        postDTO.setPosKeyword(post.getPosKeyword());
        if (post.getBackground().contains("本科")){
            postDTO.setBackground(1);
        } else if (post.getBackground().equals("不限")) {
            postDTO.setBackground(0);
        }else if (post.getBackground().contains("硕士")){
            postDTO.setBackground(2);
        }else {
            postDTO.setBackground(3);
        }
        if (post.getSexDemand().equals("男")){
            postDTO.setSexDemand("1");
        }else if (post.getSexDemand().equals("女")){
            postDTO.setSexDemand("2");
        }else {
            postDTO.setSexDemand("0");
        }
        postDTO.setJobBenefits(post.getJobBenefits());
        postDTO.setWorkPlace(post.getWorkPlace());
        int count = postMapper.update(postDTO);
        return count;
    }

    @Override
    public int delete(Integer id, Integer jrId) {
        return postMapper.delByJrId(id,jrId);
    }

    @Override
    public Post findByPostId(Integer id) {
        return postMapper.findAllByPostId(2);
    }

    @Override
    public BossDTO findById(Integer id) {

        BossDTO bossDTO = new BossDTO();
        Post post = postMapper.findAllByPostId(id);

        bossDTO.setJrId(post.getJrId());
        bossDTO.setPosName(post.getPosName());
        bossDTO.setExpDemand(post.getExpDemand());
        bossDTO.setPosType(post.getPosType());

//        Integer jrId = bossDTO.getJrId();
//        bossDTO.setJrId(jrId);
        bossDTO.setPosDescription(post.getPosDescription());
        bossDTO.setPosKeyword(post.getPosKeyword());
        bossDTO.setWorkPlace(post.getWorkPlace());
        bossDTO.setPayFloor(post.getPayFloor());
        bossDTO.setPayUpper(post.getPayUpper());
        bossDTO.setRecruitmentAgency(post.getRecruitmentAgency());
        System.out.println(post.getBackground() + "========================");
//        bossDTO.setBackground(post.getBackground());

        if (Integer.parseInt(post.getBackground()) == 0) {
            bossDTO.setBackground("学历不限");
        } else if (Integer.parseInt(post.getBackground()) == 1) {
            bossDTO.setBackground("本科以上");
        } else if (Integer.parseInt(post.getBackground()) == 2) {
            bossDTO.setBackground("硕士以上");
        } else if (Integer.parseInt(post.getBackground()) == 3) {
            bossDTO.setBackground("博士以上");
        }

        JobRecruiter jobRecruiter = recruiterMapper.findByRecruiterId(post.getJrId());
        bossDTO.setCompanyName(jobRecruiter.getCompanyName());
        bossDTO.setCompanyIntro(jobRecruiter.getCompanyIntro());
        bossDTO.setSetUpTime(jobRecruiter.getSetUpTime());
        bossDTO.setRegisterLocation(jobRecruiter.getRegisterLocation());
        bossDTO.setCompanyType(jobRecruiter.getCompanyType());

        bossDTO.setJrName(jobRecruiter.getJrName());
        bossDTO.setJrType(jobRecruiter.getJrType());
        bossDTO.setJrAvatar(jobRecruiter.getJrAvatar());

//        bossDTO.setEmployeesQuantity(jobRecruiter.getEmployeesQuantity());
        if (jobRecruiter.getEmployeesQuantity() > 0 && jobRecruiter.getEmployeesQuantity() < 100) {
            bossDTO.setEmployeesQuantity("0-99人");
        } else if (jobRecruiter.getEmployeesQuantity() > 100 && jobRecruiter.getEmployeesQuantity() < 500) {
            bossDTO.setEmployeesQuantity("100-199人");
        } else if (jobRecruiter.getEmployeesQuantity() >= 500 && jobRecruiter.getEmployeesQuantity() < 1000) {
            bossDTO.setEmployeesQuantity("500-999人");
        } else if (jobRecruiter.getEmployeesQuantity() >= 1000) {
            bossDTO.setEmployeesQuantity("1000人以上");
        }

        bossDTO.setLegalPerson(jobRecruiter.getLegalPerson());
        bossDTO.setContactPerson(jobRecruiter.getContactPerson());
        bossDTO.setRunState(jobRecruiter.getRunState());

        bossDTO.setRunBengin(jobRecruiter.getRunBengin());
        bossDTO.setCheckTime(jobRecruiter.getCheckTime());

        //注册地址
        bossDTO.setRegisterLocation(jobRecruiter.getRegisterLocation());
        //营业启示
        bossDTO.setRunBengin(jobRecruiter.getRunBengin());

        bossDTO.setRunOver(jobRecruiter.getRunOver());

        //s所属地区
        //统一社会信用码
        bossDTO.setUnifyCode(jobRecruiter.getUnifyCode());

        bossDTO.setCheckTime(jobRecruiter.getCheckTime());
        bossDTO.setFormerName(jobRecruiter.getFormerName());
        bossDTO.setRegistrationAuthority(jobRecruiter.getRegistrationAuthority());
        bossDTO.setBusinessScope(jobRecruiter.getBusinessScope());

        bossDTO.setRegistrationAuthority(jobRecruiter.getRegistrationAuthority());

        return bossDTO;
    }

    @Override
    public BossDTO findByRId(Integer id) {
        BossDTO bossDTO = new BossDTO();
        Post post = postMapper.findAllByPostId(id);

        bossDTO.setJrId(post.getJrId());
        bossDTO.setPosName(post.getPosName());
        bossDTO.setExpDemand(post.getExpDemand());
        bossDTO.setPosType(post.getPosType());

//        Integer jrId = bossDTO.getJrId();
//        bossDTO.setJrId(jrId);
        bossDTO.setPosDescription(post.getPosDescription());
        bossDTO.setPosKeyword(post.getPosKeyword());
        bossDTO.setWorkPlace(post.getWorkPlace());
        bossDTO.setPayFloor(post.getPayFloor());
        bossDTO.setPayUpper(post.getPayUpper());
        bossDTO.setRecruitmentAgency(post.getRecruitmentAgency());
        System.out.println(post.getBackground() + "========================");
//        bossDTO.setBackground(post.getBackground());

        if (Integer.parseInt(post.getBackground()) == 0) {
            bossDTO.setBackground("学历不限");
        } else if (Integer.parseInt(post.getBackground()) == 1) {
            bossDTO.setBackground("本科以上");
        } else if (Integer.parseInt(post.getBackground()) == 2) {
            bossDTO.setBackground("硕士以上");
        } else if (Integer.parseInt(post.getBackground()) == 3) {
            bossDTO.setBackground("博士以上");
        }

        JobRecruiter jobRecruiter = recruiterMapper.findByRecruiterId(id);
        bossDTO.setCompanyName(jobRecruiter.getCompanyName());
        bossDTO.setCompanyIntro(jobRecruiter.getCompanyIntro());
        bossDTO.setSetUpTime(jobRecruiter.getSetUpTime());
        bossDTO.setRegisterLocation(jobRecruiter.getRegisterLocation());
        bossDTO.setCompanyType(jobRecruiter.getCompanyType());

        if (jobRecruiter.getEmployeesQuantity() > 0 && jobRecruiter.getEmployeesQuantity() < 100) {
            bossDTO.setEmployeesQuantity("0-99人");
        } else if (jobRecruiter.getEmployeesQuantity() > 100 && jobRecruiter.getEmployeesQuantity() < 500) {
            bossDTO.setEmployeesQuantity("100-199人");
        } else if (jobRecruiter.getEmployeesQuantity() >= 500 && jobRecruiter.getEmployeesQuantity() < 1000) {
            bossDTO.setEmployeesQuantity("500-999人");
        } else if (jobRecruiter.getEmployeesQuantity() >= 1000) {
            bossDTO.setEmployeesQuantity("1000人以上");
        }
        bossDTO.setLegalPerson(jobRecruiter.getLegalPerson());
        bossDTO.setContactPerson(jobRecruiter.getContactPerson());
        bossDTO.setRunState(jobRecruiter.getRunState());
        //面试官名称和职位名称
        bossDTO.setJrName(jobRecruiter.getJrName());
        bossDTO.setJrType(jobRecruiter.getJrType());
        bossDTO.setJrAvatar(jobRecruiter.getJrAvatar());

        bossDTO.setRunBengin(jobRecruiter.getRunBengin());
        bossDTO.setCheckTime(jobRecruiter.getCheckTime());

        //注册地址
        bossDTO.setRegisterLocation(jobRecruiter.getRegisterLocation());
        //营业启示
        bossDTO.setRunBengin(jobRecruiter.getRunBengin());

        bossDTO.setRunOver(jobRecruiter.getRunOver());

        //s所属地区
        //统一社会信用码
        bossDTO.setUnifyCode(jobRecruiter.getUnifyCode());

        bossDTO.setCheckTime(jobRecruiter.getCheckTime());
        bossDTO.setFormerName(jobRecruiter.getFormerName());
        bossDTO.setRegistrationAuthority(jobRecruiter.getRegistrationAuthority());
        bossDTO.setBusinessScope(jobRecruiter.getBusinessScope());

        bossDTO.setRegistrationAuthority(jobRecruiter.getRegistrationAuthority());


        return bossDTO;
    }

    @Override
    public List<Post> findByJrId(Integer jrId) {

        List<Post> post = postMapper.findByJrId(jrId);
//        BossDTO bossDTO = new BossDTO();
//        List<BossDTO> bossDTOS = new ArrayList<>();
//
//        for (Post post1 : post) {
//            bossDTO.setJrId(post1.getJrId());
//
//            if (Integer.parseInt(post1.getBackground()) == 0) {
//                bossDTO.setBackground("学历不限");
//            }
//            else if (Integer.parseInt(post1.getBackground()) == 1) {
//                bossDTO.setBackground("本科以上");
//            } else if (Integer.parseInt(post1.getBackground()) == 2) {
//                bossDTO.setBackground("硕士以上");
//            } else if (Integer.parseInt(post1.getBackground()) == 3) {
//                bossDTO.setBackground("博士以上");
//            }
//            bossDTO.setPosName(post1.getPosName());
//            bossDTO.setExpDemand(post1.getExpDemand());
//            bossDTO.setWorkPlace(post1.getWorkPlace());
//            bossDTO.setPayFloor(post1.getPayFloor());
//            bossDTO.setPayUpper(post1.getPayUpper());
//
//            bossDTOS.add(bossDTO);
//        }

        return post;
    }

    @Override
    public int contByJrId(Integer jrId) {
        return postMapper.contByJrId(jrId);
    }

    @Override
    public int delById(Integer id) {
        return postMapper.delById(id);
    }

    @Override
    public List<Post> findByPosName(String posName, Integer jrId) {
//        BossDTO bossDTO = new BossDTO();
//        Post post = postMapper.findAllByPostId(jrId);
//
//
//        bossDTO.setPosName(post.getPosName());
//        bossDTO.setPayFloor(post.getPayFloor());
//        bossDTO.setPayUpper(post.getPayUpper());
//        JobRecruiter jobRecruiter = recruiterMapper.findById(jrId);
//        bossDTO.setJrName(jobRecruiter.getJrName());
//        bossDTO.setJrType(jobRecruiter.getJrType());
        return postMapper.findByPosName(posName,jrId);
    }

    @Override
    public PageInfo<Post> findByPosId(Integer pageNum, Integer pageSize,Integer jrId) {
        PageInfo<Post> pageInfo = null;
        Page<Post> page = PageHelper.startPage(pageNum,pageSize);
        List<Post> list = findByJrId(jrId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Post> findByPosName(Integer pageNum, Integer pageSize, String posName, Integer jrId) {
        PageInfo<Post> pageInfo = null;
        Page<Post> page = PageHelper.startPage(pageNum,pageSize);
        List<Post> list = findByPosName(posName,jrId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Post findByPosId(Integer id) {
        return postMapper.findAllByPostId(id);
    }

    @Override
    public List<Post> findAlls() {
        return postMapper.findAlls();
    }

    @Override
    public List<BossDTO> findAll() {
        BossDTO bossDTO = new BossDTO();

        return null;
    }
}
